﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 300.0f;
    public float jumpForce;

    // 缓存组件 不要每帧都去获取组件
    Animator _playAnimator;
    Rigidbody2D _playerRigibody;

    LayerMask _groundMask;


    // 教程第七节中使用collider2d.IsTouchingLayer(ground)判断是否在空中
    // 但是目前来看横向速度+纵向速度+纵向速度正负已经足够用于刷新状态机了
    // LayerMask ground;

    CircleCollider2D _playerCollider;

    // bool onTheGround;
    
    void Start()
    {
        _playerRigibody = GetComponent<Rigidbody2D>();
        
        _playAnimator = GetComponent<Animator>();

        _playerCollider = GetComponent<CircleCollider2D>();
        
        App.gameManager.SetPlayer(this.transform);

        _groundMask = LayerMask.GetMask("ground");

        App.gameManager.AddScore(0.0f);
    }

    // Update和FixUpdate的执行频率存在差异
    // Input之类的还是放在Update种比较好 在FixUpdate中监听Input会有漏监听或者多监听的情况
    void Update()
    {
        // 监听跳跃按键事件及处理
        InputJump();
    }

    void FixedUpdate()
    {
        // 更新横向运动状态
        Movement();
        // 设置动画参数
        UpdateAnimatorParam();
    }

    void UpdateAnimatorParam()
    {
        // 此处实现与教程第6、7节有所不同 教程采用设置running fall jump几个状态为参数 这里直接把运动数据传入作为参数
        // 给Animator设置float参数
        // 横向速度
        _playAnimator.SetFloat("speed_hori",Mathf.Abs(_playerRigibody.velocity.x));
        // 纵向速度
        _playAnimator.SetFloat("speed_ver",Mathf.Abs(_playerRigibody.velocity.y));
        // 纵向速度方向为正
        _playAnimator.SetBool("speed_ver_positive",_playerRigibody.velocity.y > 0);
    }

    void InputJump()
    {
        //跳跃
        if(Input.GetButtonDown("Jump")&&_playerCollider.IsTouchingLayers(_groundMask))
        {
            _playerRigibody.velocity = new Vector2(_playerRigibody.velocity.x,jumpForce);
        }
    }

    void Movement()
    {
        // 水平移动
        // 返回 -1~1 代表向左 向右 0表示没动 
        float horizontalMoving = Input.GetAxisRaw("Horizontal");
        
        // 返回 -1 0 1三个整形
        // float faceDirection = Input.GetAxisRaw("Horizontal");
  
        if(horizontalMoving != 0)
        {
            // 挂载了rigibody之后 用velocity去修改速度 
            // 用rb.MovePosition去修改坐标
            _playerRigibody.velocity = new Vector2(speed*Time.fixedDeltaTime*horizontalMoving,_playerRigibody.velocity.y);
            if(horizontalMoving > 0)
            {
                transform.localScale = new Vector3(1,1,1);
            }else{
                transform.localScale = new Vector3(-1,1,1);
            }
        }else{
            _playerRigibody.velocity = new Vector2(0,_playerRigibody.velocity.y);
        }
    }

    private void OnTriggerEnter2D(Collider2D collider) 
    {
        if(ReferenceEquals(collider.tag,Enum.Tag.Cherry)){
            App.gameManager.AddScore(1.0f);
            Destroy(collider.gameObject);
        }
    }

    string Frog = "Frog";

    private void OnCollisionEnter2D(Collision2D collision) {
        if(App.gameManager.IsEmeny(collision.gameObject.tag))
        {
            // 字符串比较的坑
            // print(collision.gameObject.tag == Enum.Tag.Frog);                  // 为True
            // print(ReferenceEquals(collision.gameObject.tag,Enum.Tag.Frog));    // 为False
            // print(Equals(collision.gameObject.tag,Enum.Tag.Frog));             // 为True

            // print(collision.gameObject.tag == Frog);                           // 为True
            // print(ReferenceEquals(collision.gameObject.tag,Frog));             // 为False
            // print(Equals(collision.gameObject.tag,Frog));                      // 为True

            // print(Enum.Tag.Frog == Frog);                                      // 为True
            // print(ReferenceEquals(Enum.Tag.Frog,Frog));                        // 为True
            // print(Equals(Enum.Tag.Frog,Frog));                                 // 为True
            // _playerRigibody.velocity = new Vector2(_playerRigibody.elocity.x/Mathf.Abs(_playerRigibody.velocity.x)*100,100);

            string aniname = GetCurrentAniName();
            if(aniname.Equals(Enum.AniName.Idle)||aniname.Equals(Enum.AniName.Fall))
            {
                Destroy(collision.gameObject);
                // 击杀后再小跳一段
                _playerRigibody.velocity = new Vector2(_playerRigibody.velocity.x,jumpForce*0.5f);
            }
        }
    }

    // 获取当前动画名
    public string GetCurrentAniName()
    {
        AnimatorClipInfo[] ret = _playAnimator.GetCurrentAnimatorClipInfo(0);
        return ret[0].clip.name;      
    }
}
